package com.OA.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.OA.entity.Knowledge;
import com.OA.entity.MenuQK;
import com.OA.entity.RoleQK;
import com.OA.entity.Thematic;
import com.OA.entity.User;
import com.OA.service.KnowledgeQKService;
import com.OA.util.Page;
import com.alibaba.fastjson.JSON;
/**
 * 控制层
 * @author NuiiQK
 *
 */
@Controller
@RequestMapping("user")
public class KnowledgeQKController {

	@Autowired
	private KnowledgeQKService knowledgeQKService;
	//___________________________________________________________________________________________专题管理————————————————
	/**
	 * 分页显示专题
	 * 
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "thematic.do")
	public void loadthematicByPage( String searchthenumber,String serachthename,int pageNo, int pageSize, HttpServletResponse resp) throws IOException {
		resp.setCharacterEncoding("utf-8");
		Page<Thematic> page = knowledgeQKService.loadthematicByPage(new Page<Thematic>(pageNo, pageSize), searchthenumber, serachthename);
		String json = JSON.toJSONString(page);
		resp.getWriter().write(json);
		resp.getWriter().flush();
		resp.getWriter().close();
	}
	
	/**
	 * 插入专题数据
	 * 
	 * @param thenumber
	 * @param thename
	 * @param thestatus
	 * @param theremarks
	 * @param resp
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "saveloadthematic.do")
	public void saveloadthematic(String thenumber,String thename, String thestatus, String theremarks,
			HttpServletResponse resp) throws IOException {
		System.out.println("============专题请求===插入数据==saveloadthematic.do=============");
		resp.setCharacterEncoding("UTF-8");
		Thematic thematic = new Thematic(thenumber,thename, thestatus, theremarks);
		knowledgeQKService.saveloadByThematic(thematic);
		resp.getWriter().write("{\"result\":\"OK\"}");
		resp.getWriter().flush();
		resp.getWriter().close();
	}
	/**
	 * 通过主键加载对象
	 * @param theid
	 * @param resp
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "loadThematicTheid.do")
	public void loadThematicTheid(int theid,HttpServletResponse resp) throws IOException {
		System.out.println("============专题请求==表单回填=加载数据==loadThematicTheid.do=============");
		resp.setCharacterEncoding("UTF-8");
		Thematic thematic = knowledgeQKService.loadCartBytheId(Integer.valueOf(theid));
		resp.setCharacterEncoding("utf-8");
		resp.getWriter().write(JSON.toJSONString(thematic));
		resp.getWriter().flush();
		resp.getWriter().close();
	}
	/**
	 * 通过主键修改对象
	 * @param theid
	 * @param thenumber
	 * @param thename
	 * @param thestatus
	 * @param theremarks
	 * @param resp
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "updateThematicTheid.do")
	public void updateThematicTheid(String theid,String thenumber, String thename, String thestatus, String theremarks,HttpServletResponse resp) throws IOException {
		System.out.println("============专题请求===修改数据==updateThematicTheid.do=============");
		resp.setCharacterEncoding("utf-8");
		Thematic thematic = new Thematic(Integer.valueOf(theid),thenumber, thename, thestatus, theremarks);
		knowledgeQKService.updateByThematic(thematic);
		resp.getWriter().write("{\"result\":\"OK\"}");
		resp.getWriter().flush();
		resp.getWriter().close();
	}
	
	/**
	 * 删除专题数据
	 * @param temp
	 * @param resp
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "deleteThematicTheid.do")
	public void deleteThematicTheid(@RequestParam(value="temp[]") Integer[] temp ,HttpServletResponse resp) throws IOException {
		System.out.println("============专题请求===删除数据==deleteThematicTheid.do=============");
		for (int i = 0; i < temp.length; i++) {
			knowledgeQKService.remove(temp[i]);
		}
		resp.getWriter().write("{\"result\":\"OK\"}");
		resp.getWriter().flush();
		resp.getWriter().close();
	}
	/**
	 * 通过当前用户登录id加载他拥有的菜单权限
	 * @param user
	 * @param req
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "menus.do")
	public String menus(User user,HttpServletRequest req) throws IOException {
		System.out.println("============菜单请求==menus.do=============");
		List<MenuQK> menus = knowledgeQKService.loadMenusByUserId(user.getUserId());
		req.getSession().setAttribute("menus", menus);
		return "nuiiqk/zhishi";
	}
	/**
	 * 添加根节点
	 * @param menu
	 * @param resp
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "addjs.do")
	public void node(MenuQK menu,HttpServletResponse resp ) throws IOException {
		System.out.println("============节点请求==menus.do=============");
		if(menu.getMenuid() != -1){
			if("".equals(menu.getMenuUrl())){
				menu.setMenuUrl(null);
			}
		}
		//插入菜单
		knowledgeQKService.insertMenuTable(menu);
		//获取新的菜单ID
		int menuid = knowledgeQKService.loadMenuById(menu.getMenuname());
		System.out.println(menuid);
		//查询角色表名单
		List<RoleQK> roleList = knowledgeQKService.loadRole();
		//循环插入关系表
		for (RoleQK roleid : roleList) {
			if(roleid.getRoleid()==7){
				knowledgeQKService.insertRoleMenu(roleid.getRoleid(), menuid);
			}
		}
		resp.getWriter().write("{\"result\":\"OK\"}");
		resp.getWriter().flush();
		resp.getWriter().close();
	}
	/**
	 * 根据ID修改节点信息
	 * @param menuid
	 * @param resp  未用
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "loadmenuid.do")
	public void loadmenuid(MenuQK menuid,HttpServletResponse resp ) throws IOException {
		System.out.println("============通过ID获取菜单信息==loadmenuid.do=============");
		resp.setCharacterEncoding("UTF-8");
		List<MenuQK> list = knowledgeQKService.loadmenuid(menuid.getMenuid());
		resp.getWriter().write(JSON.toJSONString(list));
		resp.getWriter().flush();
		resp.getWriter().close();
	}
	
	/**
	 * 根据ID修改节点信息
	 * @param menuid
	 * @param resp  
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "updatemenu.do")
	public void updatamenu(MenuQK menu,HttpServletResponse resp ) throws IOException {
		System.out.println("============修改菜单节点==updatemenu.do=======");
		resp.setCharacterEncoding("UTF-8");
		knowledgeQKService.updataMenuid(menu);
		resp.getWriter().write("{\"result\":\"OK\"}");
		resp.getWriter().flush();
		resp.getWriter().close();
	}
	
	/**
	 * 根据ID删除节点信息
	 * @param menuid
	 * @param resp  
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "menudeletemenus.do")
	public void menudeletemenus(int menuid,HttpServletResponse resp ) throws IOException {
		System.out.println("============删除菜单节点==deletemenus.do=============");
		resp.setCharacterEncoding("UTF-8");
		knowledgeQKService.delteRolemenuMenuid(menuid);
		knowledgeQKService.deleteMenuid(menuid);
		resp.getWriter().write("{\"result\":\"OK\"}");
		resp.getWriter().flush();
		resp.getWriter().close();
	}
	/**
	 * 显示知识查看
	 * @param pageNo
	 * @param pageSize
	 * @param resp
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "knowledgeall.do")
	public void knowledgeall(int pageNo, int pageSize,HttpServletResponse resp ) throws IOException {
		System.out.println("============知识查看==knowledgeall.do=============");
		Page<Knowledge> page = knowledgeQKService.loadKnowledgeByPage(new Page<Knowledge>(pageNo, pageSize));
		resp.setCharacterEncoding("UTF-8");
		String json = JSON.toJSONString(page);
		resp.getWriter().write(json);
		resp.getWriter().flush();
		resp.getWriter().close();
	}
	/**
	 * 显示我的知识
	 * @param pageNo
	 * @param pageSize
	 * @param resp
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "myknowledgeall.do")
	public void myknowledgeall(int pageNo, int pageSize,HttpServletResponse resp ) throws IOException {
		System.out.println("============我的知识查看==myknowledgeall.do=============");
		Page<Knowledge> page = knowledgeQKService.loadmyKnowledgeByPage(new Page<Knowledge>(pageNo, pageSize));
		resp.setCharacterEncoding("UTF-8");
		String json = JSON.toJSONString(page);
		resp.getWriter().write(json);
		resp.getWriter().flush();
		resp.getWriter().close();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 查询专题数据没有分页的
	 * 
	 * @param resp
	 * @throws IOException
	 */
//	@ResponseBody
//	@RequestMapping(value = "thematic.do")
//	public void thematic(int pageNo, int pageSize, HttpServletResponse resp) throws IOException {
//		System.out.println("============专题请求===展示数据==thematic.do=============");
//		Page<Thematic> list = knowledgeQKService.selectThematicByAllPage(pageNo, pageSize);
//		resp.setCharacterEncoding("UTF-8");
//		String json = JSON.toJSONString(list);
//		resp.getWriter().write(json);
//		resp.getWriter().flush();
//		resp.getWriter().close();
//	}
}
